package org.appfuse.dao.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.dao.JtcyjzyshgxDAO;
import org.appfuse.model.Gzxx;
import org.appfuse.model.Jtcyjzyshgx;
import org.hibernate.FlushMode;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class Jtcyjzyshgx.
 * 
 * @see org.appfuse.model.Jtcyjzyshgx
 * @author MyEclipse - Hibernate Tools
 */
public class JtcyjzyshgxDAOHibernate extends BaseDaoHibernate implements
		JtcyjzyshgxDAO {

	private static final Log log = LogFactory
			.getLog(JtcyjzyshgxDAOHibernate.class);

	public void save(Jtcyjzyshgx transientInstance) {
		log.debug("saving Jtcyjzyshgx instance");
		try {
			getSession().setFlushMode(FlushMode.AUTO);
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	

	public void update(Jtcyjzyshgx transientInstance) {
		log.debug("saving Jtcyjzyshgx instance");
		try {
			getSession().setFlushMode(FlushMode.AUTO);
			getSession().update(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	

	public void delete(Jtcyjzyshgx persistentInstance) {
		log.debug("deleting Jtcyjzyshgx instance");
		try {
			getSession().setFlushMode(FlushMode.AUTO);
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Jtcyjzyshgx findById(String a00, Integer id) {
		  log.debug("getting Gzxx instance with id: " + id);
	        try {
	        	String queryString = "from Jtcyjzyshgx as model where model.basicInfo.id"
					 + "= ? " + " and model.id = ?";
				Query queryObject = getSession().createQuery(queryString);
				queryObject.setParameter(0, a00);
				queryObject.setParameter(1, id);
				return (Jtcyjzyshgx) queryObject.uniqueResult();
	        } catch (RuntimeException re) {
	            log.error("get failed", re);
	            throw re;
	        }
	}

	public List findByExample(Jtcyjzyshgx instance) {
		log.debug("finding Jtcyjzyshgx instance by example");
		try {
			List results = getSession().createCriteria(
					"org.appfuse.model.Jtcyjzyshgx").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Jtcyjzyshgx instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Jtcyjzyshgx as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public Jtcyjzyshgx merge(Jtcyjzyshgx detachedInstance) {
		log.debug("merging Jtcyjzyshgx instance");
		try {
			Jtcyjzyshgx result = (Jtcyjzyshgx) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Jtcyjzyshgx instance) {
		log.debug("attaching dirty Jtcyjzyshgx instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Jtcyjzyshgx instance) {
		log.debug("attaching clean Jtcyjzyshgx instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public Integer newId(String a00) {
		log.debug("generate a new id");
		try {
			String queryString = "select max(id) from Jtcyjzyshgx as model where model.basicInfo.id "
					+ "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, a00);
			Integer maxId = (Integer) queryObject.uniqueResult();
			if (maxId != null) {
				maxId = maxId + 1;
			} else {
				maxId = 1;
			}
			log.debug("generate new id successful");
			return maxId;

		} catch (RuntimeException re) {
			log.error("generate new id failed", re);
			throw re;
		}
	}

	public List findByA00(String a00) {
		log.debug("generate a new id");
		try {
			String queryString = " from Jtcyjzyshgx as model where model.basicInfo.id "
					+ "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, a00);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("generate new id failed", re);
			throw re;
		}
	}
}